package com.party.mobile.web.controller.mall;

import com.party.common.paging.Page;
import com.party.common.utils.PartyCode;
import com.party.common.utils.StringUtils;
import com.party.core.exception.BusinessException;
import com.party.core.model.system.TargetType;
import com.party.mobile.biz.mall.MallStoreBizService;
import com.party.mobile.web.annotation.VisitCollect;
import com.party.mobile.web.dto.AjaxResult;
import com.party.mobile.web.dto.mall.output.MallSiftGoodsOutput;
import com.party.mobile.web.dto.mall.output.MallStoreOutput;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 商城店铺控制器
 * Created by yifeng
 *
 * @date 2018/4/10 0010
 * @time 上午 11:32
 */
@Controller
@RequestMapping("mall/store")
public class MallStoreController {
    @Autowired
    private MallStoreBizService mallStoreBizService;

    Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * 获取首页信息
     * @return
     */
    @ResponseBody
    @RequestMapping("mainIndex")
    @VisitCollect(module = TargetType.MALL)
    public AjaxResult mainIndex() {
        try {
            MallStoreOutput mallStoreOutput = mallStoreBizService.mainIndex();
            return AjaxResult.success(mallStoreOutput);
        } catch (BusinessException e) {
            return AjaxResult.error(e.getCode(), e.getMessage());
        } catch (Exception e) {
            logger.error("获取店铺主页异常", e);
            return AjaxResult.error(100, "获取店铺主页异常");
        }
    }

    /**
     * 首页更多
     * @param storeId: 店铺id
     * @param model: 模块id
     * @param page
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/more", method = RequestMethod.GET)
    @VisitCollect(module = TargetType.MALL)
    public AjaxResult more(String storeId, Integer model, Page page) {
        try {
            if (StringUtils.isEmpty(storeId) || null == model) {
                return AjaxResult.error(PartyCode.PARAMETER_ILLEGAL, "参数不能为空");
            }
            List<MallSiftGoodsOutput> goods = mallStoreBizService.getGoods(storeId, model, page);
            return AjaxResult.success(goods, page);
        } catch (Exception e) {
            logger.error("获取店铺更多信息异常", e);
            return AjaxResult.error(100, "获取店铺更多信息异常");
        }
    }

    /**
     * 根据配置的商品对应的分类获取商品
     *
     * @param categoryId 分类编号
     * @param storeId    店铺编号
     * @return
     */
    @ResponseBody
    @RequestMapping("findByCategoryId")
    public AjaxResult findByCategoryId(String categoryId, String storeId) {
        if (StringUtils.isEmpty(categoryId)) {
            return AjaxResult.error(PartyCode.PARAMETER_ILLEGAL, "分类编号不能为空");
        }
        try {
            List<MallSiftGoodsOutput> goodsListOutputs = mallStoreBizService.findByCategoryId(categoryId, storeId);
            return AjaxResult.success(goodsListOutputs);
        } catch (Exception e) {
            logger.error("获取商品异常", e);
            return AjaxResult.error(100, "获取商品异常");
        }
    }
}
